//
//  YITabBarController.m
//  一点互助
//
//  Created by rimi on 16/8/19.
//  Copyright © 2016年 shen. All rights reserved.
//

#import "YITabBarController.h"
#import "YIHome.h"
#import "YIMy.h"
#import "YIProject.h"
#import "YIInformation.h"
#import "YILogin.h"
@interface YITabBarController ()<UITabBarControllerDelegate>
@property(nonatomic,strong)NSMutableArray *navArr;
@end

@implementation YITabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
       self.delegate = self;
       [self tabBarAddSubVC:[[YIHome alloc]init] title:@"首页" imageName:@"首页"];
       [self tabBarAddSubVC:[[YIProject alloc]init] title:@"项目" imageName:@"项目"];
       [self tabBarAddSubVC:[[YIInformation alloc]init] title:@"消息" imageName:@"消息"];
       [self tabBarAddSubVC:[[YIMy alloc]init] title:@"我的" imageName:@"我的"];
       self.viewControllers = self.navArr;
}

-(void)tabBarAddSubVC:(UIViewController *)VC title:(NSString *)title imageName:(NSString *)imageName{
       VC.tabBarItem.title = title;
       [VC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:0];
       [VC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:123/255.0 green:231/255.0 blue:206/255.0 alpha:1]} forState:4];
       VC.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon-%@",imageName]];
       VC.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"icon-%@",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:VC];
       
       [nav.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
       
       nav.navigationBar.barTintColor = HEADER_COLOR;
       nav.navigationBar.tintColor = [UIColor whiteColor];
       [self.navArr addObject:nav];
}

//代理方法,判断点击的是哪一个
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
       NSInteger index = 0;
       if ([viewController.tabBarItem.title isEqualToString:@"消息"]) {
              //如果用户ID存在的话，说明已登陆
              if (index == 1) {
                     return YES;
              }else{
                     //跳到登录页面 CCPLoginVC
                     YILogin *login = [[YILogin alloc]init];
                            login.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
                            [self presentViewController:login animated:YES completion:nil];
                     return NO;
              }
       }else {
              
              return YES;
              
       }
       
       
}
-(NSMutableArray *)navArr{
       if (!_navArr) {
              _navArr = [NSMutableArray array];
       }
       return _navArr;
}
@end
